package com.sicau.smarttraceplatform.config.security;

import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.cors.CorsUtils;

import javax.annotation.Resource;

import static com.sicau.smarttraceplatform.common.constants.CommonURLConstants.USER_PREFIX;

/**
 * @author sxt
 * @version : 1.0.0
 */
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
@Order(SecurityProperties.BASIC_AUTH_ORDER)
public class MSecurityConfig extends WebSecurityConfigurerAdapter {


    @Resource
    private SecurityHandler securityHandler;

    @Resource
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .requestMatchers(CorsUtils::isPreFlightRequest)
                .permitAll()
                .antMatchers(
//                        "/v2/**",
//                        "/swagger-ui.html#!/**",
//                        "/swagger-ui.html#/**",
//                        "/swagger-ui.html/**",
//                        "/swagger-resources/**",
//                        "/webjars/**",
//                        "/loginPage/**",
//
//                        //以下是公共接口
//                        "/v1/**/h5/**",
//                        "/v1/**/h5"
                        "/**"
                ).permitAll()
                .anyRequest()
                .authenticated()
                .and()
//                .addFilterBefore(loginFilter(), UsernamePasswordAuthenticationFilter.class)
//                .addFilterBefore(new JWTAuthenticationFilter(userDetailsService), UsernamePasswordAuthenticationFilter.class)
                .formLogin()
                .and()
                .cors()
                .and()
                .csrf().disable() //关闭CSRF
                .formLogin().loginPage(USER_PREFIX + "/authentication/require")
                .loginProcessingUrl("/login")
//                .successHandler(securityHandler)
                .successForwardUrl(USER_PREFIX + "/loginSuccess") //成功登陆后
                .failureUrl(USER_PREFIX + "/loginError").permitAll()
                .and()
                .logout().logoutUrl("/logout")
                .logoutSuccessHandler(securityHandler)
                .and()
                //配置没有权限的自定义处理类
                .exceptionHandling().accessDeniedHandler(securityHandler); /*emmm 没生效 暂不清楚原因*/
    }

    @Resource
    private AuthenticationProvider provider;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(provider);
    }


}
